var util = require("../../utils/util.js");
var app = getApp();

Page({

  //奖品配置
  awardsConfig: {
    chance: true,
    awards: []
  },

  data: {
    awardsList: [],
    animationData: {},
    btnDisabled: false,
    showMask: true,
    showPrize: true,
    showCode: true,
    code: '',
    frequency: 0,
    prizeImg: '',
    goodsId: null
  },

  onLoad: function (options) {
    let token = wx.getStorageSync('token')
    let that = this
    that.setData({
      toLogin: !Boolean(token)
    })
  },

  onShow: function () {
    this.onLoad()
    app.post('Prize/index', {
      token: wx.getStorageSync('token')
    }).then(res => {
      this.setData({
        frequency: res.data.result_count
      })
    })
  },

  onReady: function (e) {
    this.drawAwardRoundel();
    //分享
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  //画抽奖圆盘
  drawAwardRoundel: function () {
    var awards = [];
    app.post('Prize/index', {
      token: wx.getStorageSync('token')
    }).then(res => {
      awards = res.data.result_date
      var awardsList = [];
      var turnNum = 1 / awards.length; // 文字旋转 turn 值

      // 奖项列表
      for (var i = 0; i < awards.length; i++) {
        awardsList.push({
          turn: i * turnNum + 'turn',
          lineTurn: i * turnNum + turnNum / 2 + 'turn',
          award: awards[i].name,
          imgUrl: awards[i].p_pic,
          id: awards[i].p_id
        });
      }
      this.setData({
        btnDisabled: this.awardsConfig.chance ? false : true,
        awardsList: awardsList,
        frequency: res.data.result_count
      });
    })
  },

  //发起抽奖
  playReward: function () {
    if (this.data.frequency == 0) {
      wx.showToast({
        title: '您没有抽奖次数',
        icon: 'none'
      })
    } else {
      var awardIndex = null; //中奖index
      var runNum = 8; //旋转8周
      var duration = 4000; //时长
      app.post('Prize/play', {
        token: wx.getStorageSync('token')
      }).then(res => {
        for (let i = 0; i < this.data.awardsList.length; i++) {
          if (this.data.awardsList[i].id == res.data.result_data.p_id) {
            awardIndex = i
          }
        }
        this.data.goodsId = res.data.result_data.p_id
        this.setData({
          prizeImg: res.data.result_data.p_pic,
        })
      }).then(() => {

        // 旋转角度
        this.runDeg = this.runDeg || 0;
        this.runDeg = this.runDeg + (360 - this.runDeg % 360) + (360 * runNum - awardIndex * (360 / 6))

        //创建动画
        var animationRun = wx.createAnimation({
          duration: duration,
          timingFunction: 'ease'
        })
        animationRun.rotate(this.runDeg).step();
        this.setData({
          animationData: animationRun.export(),
          btnDisabled: true
        });
      })

      // 中奖提示
      var awardsConfig = this.awardsConfig;
      setTimeout(function () {
        // wx.showModal({
        //   title: '恭喜',
        //   content: '获得' + (awardsConfig.awards[awardIndex].name),
        //   showCancel: false
        // });
        this.setData({
          showPrize: false,
          showMask: false,
          btnDisabled: false,
        });
      }.bind(this), duration);
      app.post('Prize/index', {
        token: wx.getStorageSync('token')
      }).then(res => {
        this.setData({
          frequency: res.data.result_count
        })
      })
    }
  },

  // 关闭弹框
  closeMask: function () {
    this.setData({
      showMask: true,
      showPrize: true,
      showCode: true,
      btnDisabled: false,
    })
  },

  // 立即领取
  toReceive: function () {
    // wx.navigateTo({
    //   url: '/pages/exchange/exchange?type=receive&gid=' + this.data.goodsId,
    // })
    this.closeMask()
  },

  // 获取次数
  getFrequency: function () {
    this.setData({
      showMask: false,
      showCode: false
    })
  },

  codeVal: function (e) {
    this.data.code = e.detail.value
  },

  // 提交序列码
  submitCode: function () {
    app.post('Prize/check', {
      token: wx.getStorageSync('token'),
      number: this.data.code
    }).then(res => {
      wx.showToast({
        title: res.data.result_code == 100 ? res.data.result_message : '序列号不存在',
        icon: 'none',
        success: () => {
          if (res.data.result_code == 100) {
            app.post('Prize/index', {
              token: wx.getStorageSync('token')
            }).then(res => {
              this.setData({
                frequency: res.data.result_count
              })
            })
            this.closeMask()
          }
        }
      })
    })
  },

  // 中奖纪录
  toRecord: function () {
    wx.navigateTo({
      url: '/pages/winningRecord/winningRecord',
    })
  },

  onShareAppMessage: function () {
    var that = this;
    return util.doShare("大转盘抽奖", "pages/zp/zp", that);
  }
})